package com.briup.pts.service.impl;

import com.briup.pts.config.exception.ServiceException;
import com.briup.pts.config.response.ResultCode;
import com.briup.pts.domain.bean.Animal;
import com.briup.pts.domain.bean.Hurdles;
import com.briup.pts.domain.bean.IndexRecord;
import com.briup.pts.domain.vo.BatchVO;
import com.briup.pts.domain.vo.DiseaseRecordVO;
import com.briup.pts.domain.vo.SourceVO;
import com.briup.pts.mapper.*;
import com.briup.pts.service.SourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SourceServiceImpl implements SourceService {

    private final AnimalMapper animalMapper;
    private final HurdlesMapper hurdlesMapper;
    private final BatchMapper batchMapper;
    private final DiseaseRecordMapper diseaseRecordMapper;
    private final IndexRecordMapper indexRecordMapper;

    @Override
    public SourceVO source(String animalId) {
        // 根据动物id查询对应的动物信息
        Animal animal = animalMapper.queryByIdWithStatus(animalId, null);
        if (animal==null){
            throw new ServiceException(ResultCode.ANIMAL_IS_DELETED);
        }
        // 根据该动物所在的栏圈id查询对应的栏圈信息
        Hurdles hurdles = hurdlesMapper.queryById(animal.getAHurdlesId());
        // 根据该动物所在的批次id查询对应的批次信息
        BatchVO batch = batchMapper.queryByIdWithQR(animal.getABatchId());
        // 根据动物id查询对应动物的所有病症记录数据
        List<DiseaseRecordVO> diseaseRecordVOS = diseaseRecordMapper.queryByAnimalId(animalId);
        // 根据动物id查询对应动物的所有指标变动记录
        List<IndexRecord> indexRecords = indexRecordMapper.queryByAnimalId(animalId);

        return SourceVO.builder()
                .animal(animal)
                .hurdles(hurdles)
                .batch(batch)
                .diseaseRecords(diseaseRecordVOS)
                .indexRecords(indexRecords)
                .build();
    }
}
